<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of soaUsers
 *
 * @author dxx
 */

namespace app\common\model\jooj_member;

use app\common\model\jooj_member\MemberIndex;
use app\common\common\Helper;

class ViewMemberIndex extends Base {

    protected $connection = ['database' => 'jooj_member'];
    protected $companyId;

    /**
     * 构造方法，用于判断表是否存在，如果不存在就创建
     * @param type $company_id
     */
    public function __construct($company_id = []) {
        $this->companyId = $company_id;
    }

    /**
     * 耦合查询会员信息
     * @param type $where
     * @return type []
     */
    public function getIndexAndvipData($where) {
        if (empty($where)) {
            return false;
        }
        $data = $this->getViewInfoByCondition($where);
        $returnData = [];
        if (!empty($data)) {
            foreach ($data as $key => $val) {
                $data[$key]['integral'] = false === empty($data[$key]['integral']) ? $data[$key]['integral'] : '0';
                $data[$key]['integral_use'] = false === empty($data[$key]['integral']) ? $data[$key]['integral_use'] : '0';
                $returnData = $data;
            }
        }
        return $returnData;
    }

    /**
     * * 获取一条view中的记录
     * $memberCodeStr只能传member_code的字符串(1 or 1,2,3,4)
     */
    public function getViewInfoByCondition($condition) {
        $viewMemberInfo = [];
        $memberIndexModel = new MemberIndex($this->companyId);
        if (array_key_exists('member_code', $condition)) {
            $stringArr = explode(',', $condition['member_code']);
            unset($condition['member_code']);
            foreach ($stringArr as $k => $v) {
                $jjUniId = $memberIndexModel->where(['member_code' => $v])->value('jj_uni_id');
                // 由于传过来的值有可能是逗号分开的字符串，有可能某个数字查到的数据为空，所以用切割字符串的方法来做
                $returnData = $this->getCompleteData(['jj_uni_id' => $jjUniId]);
            }
        } else {
            $jjUniId = $memberIndexModel->where($condition)->value('jj_uni_id');
            $returnData = $returnData = $this->getCompleteData(['jj_uni_id' => $jjUniId]);
        }

        return $returnData;
    }

    /**
     * 通过条件查出view数据
     * @param type $condition
     * @return type
     */
    public function getMemberListByKeyword($keyword, $page = 1, $pageSize = 10) {
        $where = 'name = "' . $keyword . '" or mobile ="' . $keyword . '"';
        $memberIndexModel = new MemberIndex($this->companyId);
        $jjUniIdGoupData = $memberIndexModel->where($where)->group('jj_uni_id')->column('jj_uni_id');
        $where = ['jj_uni_id' => ['in', $jjUniIdGoupData]];
        $count = count($this->getCompleteData($where));
        $list = $this->getCompleteData($where, $page, $pageSize);
        $returnData = [
            'totalCount' => $count,
            'totalPage' => $count ? ceil($count / $pageSize) : 0,
            'page' => $page,
            'pageSize' => $pageSize,
            'list' => Helper::convertUnderlineArray($list),
        ];
        return $returnData;
    }

    /**
     * 通过条件查出view数据
     * @param type $condition
     * @return type
     */
    public function getViewMemberIndexByCondition($condition = []) {
        if (empty($condition)) {
            return [];
        }
        $memberIndexModel = new MemberIndex($this->companyId);
        $jjUniId = $memberIndexModel->where($condition)->value('jj_uni_id');
        $res = $this->getCompleteData(['jj_uni_id' => $jjUniId]);
        $data = $res ? current($res) : [];
        return $data;
    }

    /**
     * 获取会员列表
     * @param type $condition
     * @return type
     */
    public function getViewMemberListByCondition($condition) {
        $memberIndexModel = new MemberIndex($this->companyId);
        $jjUniId = $memberIndexModel->where($condition)->value('jj_uni_id');
        $res = $this->getCompleteData(['jj_uni_id' => $jjUniId]);
        return $res;
    }

    /**
     * 查询完整会员数据
     * @param type array $where
     * @return type
     */
    private function getCompleteData($where, $page = 0, $pageSize = 0) {
        $memberIndexModel = new MemberIndex($this->companyId);
        $query = $memberIndexModel
                ->alias('member_index')
                ->field("group_concat(member_index.member_code separator ',') AS member_code,member_index.jj_uni_id AS jj_uni_id,group_concat(member_index.mobile separator '||') AS mobile,group_concat(member_index.name separator '||') AS name,group_concat(member_index.vip_id separator '||') AS vip_id,group_concat(member_index.gender separator '||') AS gender,group_concat(member_index.birthday separator '||') AS birthday,group_concat(member_index.header_img separator '||') AS header_img,group_concat(member_index.account_code separator '||') AS account_code,group_concat(member_index.mobile_status separator '||') AS mobile_status,group_concat(member_index_info.favour_store separator '||') AS favour_store,group_concat(member_index_info.favour_p separator '||') AS favour_p,group_concat(member_index_info.service_p separator '||') AS service_p,group_concat(member_index_info.service_store separator '||') AS service_store,group_concat(member_index_info.service_qrscene separator '||') AS service_qrscene,group_concat(member_index.cardno separator '||') AS cardno,group_concat(member_index.card_lev separator '||') AS card_lev,group_concat(mfg.grant_id SEPARATOR '||') AS grant_id, member_index_info.first_store AS first_store,member_index_info.first_p AS first_p,member_index.come_from AS come_from,vip.province AS province,vip.city AS city,vip.area AS area,vip.adress AS adress,vip.zipcode AS zipcode,vip.integral AS integral,vip.integral_use AS integral_use,vip.first_qrscene AS first_qrscene,vip.remark AS remark,member_index.add_ts AS add_ts,member_index.last_ts AS last_ts,member_index.mp_id AS mp_id,wx_union_id")
                ->join('jj_member_index_info_' . $this->companyId . ' member_index_info', 'member_index.member_code = member_index_info.member_code', 'left')
                ->join('jj_member_vip_' . $this->companyId . ' vip', 'member_index.member_code = vip.member_code', 'left')
                ->join('jj_member_face_grant_' . $this->companyId . ' mfg', 'member_index.member_code = mfg.member_code and mfg.delete_time is null', 'left')
                ->where($where)
                ->group('member_index.jj_uni_id');
        if ($page) {
            $query = $query->page($page, $pageSize);
        }
        $data = $query->select();
        if ($data) {
            $data = collection($data)->toArray();
        }
        return $data;
    }

    /**
     * 通过手机号获取会员列表
     * @param type $where
     * @return type
     */
    public function getMemberListByMobile($where) {
        $res = $this->getCompleteData($where);
        return $res;
    }

}
